package com.banciyuan.bcywebview.biz.circles.smooth.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.biz.detail.goods.GoodsDetailActivity;
import com.banciyuan.bcywebview.biz.goods.store.AllGoodsActivity;
import com.banciyuan.bcywebview.biz.write.photoselecotor.util.CommonUtils;
import com.banciyuan.bcywebview.utils.common.UIUtils;
import com.banciyuan.bcywebview.utils.currency.CurrencyUtil;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.List;

import de.greenrobot.daoexample.model.DetailGoods;

/**
 * Created by squall on 2016/12/2.
 */

public class CircleSmoothGoodsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<DetailGoods> dlist;
    private Context mContext;
    private LayoutInflater inflater;
    private int height;
    private boolean loading;
    private int min_height;
    private int max;


    public CircleSmoothGoodsAdapter(List<DetailGoods> dlist, Context mContext) {
        this.dlist = dlist;
        this.mContext = mContext;
        this.inflater = LayoutInflater.from(mContext);

        min_height = CommonUtils.getHeightPixels((Activity) mContext) - CommonUtils.getStatusPixels((Activity) mContext) - UIUtils.dip2px(94, mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        RecyclerView.ViewHolder viewHolder = null;
        switch (viewType) {
            case 0:
                viewHolder = new BlankHolder(inflater.inflate(R.layout.smooth_blank, null));
                break;
            case 1:
                viewHolder = new GoodsHolder(inflater.inflate(R.layout.goods_store_item_single, null));
                break;
            case 2:
                viewHolder = new BottomHolder(inflater.inflate(R.layout.circle_goods_smooth_bottom, null));
                break;
            default:
                viewHolder = new BlankHolder(inflater.inflate(R.layout.smooth_blank, null));
                break;
        }

        return viewHolder;

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        switch (getItemViewType(position)) {
            case 0:
                setHeaderBlankHolder((BlankHolder) holder);
                break;
            case 1:
                GoodsHolder goodsHolder = (GoodsHolder) holder;
                setGoods(goodsHolder, dlist.get(position - 1));
                setHeight(goodsHolder, position);
                break;
            case 2:
                setFooterBlankHolder((BottomHolder) holder);
                break;
        }
    }

    private void setHeight(GoodsHolder goodsHolder, int position) {
        if (height < min_height && position > max) {
            goodsHolder.itemView.measure(
                    View.MeasureSpec.makeMeasureSpec(CommonUtils.getWidthPixels((Activity) mContext) / 2, View.MeasureSpec.AT_MOST),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            boolean mod = (position - 1) % 2 == 0;
            if (mod) {
                height += goodsHolder.itemView.getMeasuredHeight();
            }
            max = position;
        }
    }

    private void setGoods(GoodsHolder goodsHolder, final DetailGoods tempD) {
        if (!TextUtils.isEmpty(tempD.getCover())) {
            ImageLoader.getInstance().displayImage(tempD.getCover(), goodsHolder.pic, ImageOptions.options);
        }
        if (!TextUtils.isEmpty(tempD.getTitle())) {
            goodsHolder.title.setText(tempD.getTitle());
        }
        if (!TextUtils.isEmpty(tempD.getUname())) {
            goodsHolder.name.setText(String.format(mContext.getString(R.string.power_by), tempD.getUname()));
        }
        if (!TextUtils.isEmpty(tempD.getBasic_price())) {
            goodsHolder.price.setText(String.format(mContext.getString(R.string.price_format), CurrencyUtil.getMoney(tempD.getBasic_price())));
        }
        ((View) goodsHolder.pic.getParent()).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoUtil.gotoAct(mContext, GoodsDetailActivity.class, tempD.getZg_id());
            }
        });
    }

    private void setHeaderBlankHolder(BlankHolder blankHolder) {
        int offset = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            offset = UIUtils.dip2px(20, mContext);
        }
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, UIUtils.dip2px(212, mContext) + offset);
        blankHolder.blank.setLayoutParams(layoutParams);
    }

    private void setFooterBlankHolder(BottomHolder bottomHolder) {
        bottomHolder.go_goods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoUtil.gotoAct(mContext, AllGoodsActivity.class, 0);
            }
        });
        int footer = min_height - height;
        RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, footer > 0 ? footer : 1);
        bottomHolder.blank.setLayoutParams(layoutParams);
        if (loading) {
            bottomHolder.loading.setVisibility(View.VISIBLE);
        } else {
            bottomHolder.loading.setVisibility(View.GONE);
        }
        if (getItemCount() == 2) {
            bottomHolder.empty.setVisibility(View.VISIBLE);
        } else {
            bottomHolder.empty.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return dlist.size() + 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        } else if (position == dlist.size() + 1) {
            return 2;
        } else {
            return 1;
        }
    }

    private class BlankHolder extends RecyclerView.ViewHolder {

        View blank;

        public BlankHolder(View itemView) {
            super(itemView);
            blank = itemView.findViewById(R.id.blank);
        }
    }

    private class BottomHolder extends RecyclerView.ViewHolder {

        RelativeLayout blank;
        LinearLayout empty;
        LinearLayout loading;
        TextView go_goods;


        public BottomHolder(View itemView) {
            super(itemView);
            blank = (RelativeLayout) itemView.findViewById(R.id.smooth_bottom);
            empty = (LinearLayout) itemView.findViewById(R.id.event_empty);
            loading = (LinearLayout) itemView.findViewById(R.id.progressbar_loading_layout);
            go_goods = (TextView) itemView.findViewById(R.id.go_goods);
        }
    }

    private class GoodsHolder extends RecyclerView.ViewHolder {

        public ImageView pic;
        public TextView title;
        public TextView name;
        public TextView price;

        public GoodsHolder(View itemView) {
            super(itemView);
            pic = (ImageView) itemView.findViewById(R.id.goods_pic_one);
            title = (TextView) itemView.findViewById(R.id.goods_title_one);
            name = (TextView) itemView.findViewById(R.id.goods_user_one);
            price = (TextView) itemView.findViewById(R.id.goods_price_one);
        }
    }

    public void setMax(int max) {
        this.max = max;
    }

    public void setLoading(boolean loading) {
        this.loading = loading;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}
